﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.18
 * 描 述：科目选择
 */
var dfopid = request('dfopid');
var selectValue = request('selectValue');
var selectedRow;
var refreshGirdData;
var acceptClick;

var companyId = request("companyId");
var ztId = request("ztId");
var kjnd = request("kjnd");
var keyword = request("keyword");

var bootstrap = function ($, learun) {
    "use strict";
    var kmitem = {value: '', text: ''};

    var page = {
        init: function () {
            page.bind();
            page.initGrid();
            page.search({keyword: keyword});
        },
        bind: function () {
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });
            $("#txt_Keyword").keypress(function (even) {
                if (even.which == 13) {
                    var keyword = $(this).val();
                    page.search({keyword: keyword});
                }
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 新增
            $('#lr_add').on('click', function () {
                learun.layerForm({
                    id: 'form',
                    title: '新增方案',
                    url: config.webportal_server + 'pages/finance/pz_import/form.html?access_token=' + config.getToken().access_token + '&companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd,
                    width: 500,
                    height: 340,
                    callBack: function (id) {
                        return top[id].acceptClick(refreshGirdData);
                    }
                });
            });
            // 编辑
            $('#lr_edit').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                var kjkmJ = selectedRow.kjkmJ;
                var kjkmD = selectedRow.kjkmD;
                var name = selectedRow.name;
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'form',
                        title: '编辑方案',
                        url: config.webportal_server + 'pages/finance/pz_import/form.html?keyValue=' + keyValue + '&kjkmJ=' + kjkmJ + '&kjkmD=' + kjkmD + '&name=' +  encodeURI(encodeURI(name)) + '&access_token=' + config.getToken().access_token + '&companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd,
                        width: 500,
                        height: 340,
                        callBack: function (id) {
                            return top[id].acceptClick(refreshGirdData);
                        }
                    });
                }
            });
            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('该动作执行后此项将彻底被删除，是否确认删除该项?', function (res) {
                        if (res) {
                            learun.deleteForm(config.base_server() + 'pz_imports/delete/' + keyValue + '?access_token=' + config.getToken().access_token, {keyValue: keyValue}, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'pz_imports/list?access_token=' + config.getToken().access_token,
                headData: [
                    {label: '方案名称', name: 'name', width: 225, align: 'left'},
                    {label: "借方科目", name: "kjkmJ", width: 150, align: "center"},
                    {label: "贷方科目", name: "kjkmD", width: 150, align: "center"},
                    {
                        label: '更新时间', name: 'updateTime', width: 140, align: 'center',
                        formatter: function (cellvalue) {
                            return learun.formatDate(cellvalue, 'yyyy-MM-dd hh:mm:ss');
                        }
                    },
                ],
                dblclick: function (row) {
                    parent.$(".layui-layer-btn0")[0].click();

                    //关闭layer弹出层
                    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                },
                isPage: true,
                reloadSelected: true,
                isTree: false,
                mainId: 'id'
            });
        },
        search: function (param) {
            param = param || {};
            param.companyId = companyId;
            param.ztId = ztId;
            param.kjnd = kjnd;
            // param.keyword = keyword;
            $('#gridtable').jfGridSet('reload', param);
        }
    };
    refreshGirdData = function () {
        page.search();
    };

    // 保存数据
    acceptClick = function (callBack) {
        var row = $('#gridtable').jfGridGet('rowdata');
        kmitem = {kjkmJ: row.kjkmJ, kjkmD: row.kjkmD};
        callBack(kmitem, dfopid);
        return true;
    };

    page.init();
}